package screens;

import log.Logger;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import refresh.RefreshData;
import runnables.FilesAndNewsRunnable;
import runnables.GroupRunnable;
import strings.WebcampusResource;
import util.LookAndFeel;
import webservice.WebServiceURLs;
import beans.Course;
import beans.Grade;
import customfields.GradeField;
import customfields.InformationField;
import customfields.OptionField;
import customfields.WhiteLabelField;

public class GradesScreen extends TransitionScreen implements WebcampusResource{//MainScreen{
	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private Course course;

	public GradesScreen(Course cours, int transitionType) {
		super(transitionType);
		
		Logger.log("creating grades screen");

		this.course=cours;

		// Main vertical field manager
		VerticalFieldManager vfm = new VerticalFieldManager();
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), background_bitmap.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		// Course information
		vfm.add(new WhiteLabelField(resources.getString(GENERALINFO_LABEL), Field.NON_FOCUSABLE));
		vfm.add(new LabelField(""));

		GradeField materia=new GradeField(course.getName(), Field.FIELD_HCENTER | Field.FOCUSABLE );
		materia.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
		vfm.add(materia);
		InformationField aula=new InformationField("Aula: "+course.getClassroom(), LookAndFeel.icono_aula, Field.FIELD_HCENTER | Field.FOCUSABLE);
		aula.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
		vfm.add(aula);
		InformationField dias=new InformationField(course.getDays()+" | "+course.getTime(), LookAndFeel.icono_fecha, Field.FIELD_HCENTER | Field.FOCUSABLE);
		dias.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
		vfm.add(dias);
		InformationField fechaFinal=new InformationField(course.getExamDate(), LookAndFeel.icono_fecha_final, Field.FIELD_HCENTER | Field.FOCUSABLE);
		fechaFinal.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
		vfm.add(fechaFinal);
		InformationField asistencia=new InformationField(course.getAttendance()+" %", LookAndFeel.icono_asistencia, Field.FIELD_HCENTER | Field.FOCUSABLE);
		asistencia.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
		vfm.add(asistencia);

		// Separator
		vfm.add(new LabelField(""));
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		// Grades
		vfm.add(new WhiteLabelField(resources.getString(GRADES_LABEL), Field.NON_FOCUSABLE));
		vfm.add(new LabelField(""));

		GradeField gradefield=null;
		for (int i=0; i<this.course.getGrades().size(); i++){
			Grade g = (Grade) this.course.getGrades().elementAt(i);
			gradefield = new GradeField(g.getActivity(),g.getGrade(), Field.FIELD_HCENTER | Field.FOCUSABLE);
			gradefield.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 200));
			vfm.add(gradefield);
		}

		// Separator
		vfm.add(new LabelField(""));
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		// Options
		vfm.add(new WhiteLabelField(resources.getString(OPTIONS_LABEL), Field.NON_FOCUSABLE));
		vfm.add(new LabelField(""));

		OptionField descargas=new OptionField(resources.getString(DOWNLOADS_LABEL), Field.FIELD_HCENTER | Field.FOCUSABLE);
		descargas.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color, LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		descargas.setChangeListener(downloadsListener);
		vfm.add(descargas);
		OptionField novedades=new OptionField(resources.getString(NEWS_LABEL), Field.FIELD_HCENTER | Field.FOCUSABLE);
		novedades.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color, LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		novedades.setChangeListener(newsListener);
		vfm.add(novedades);
		OptionField grupos=new OptionField(resources.getString(GROUPS_LABEL), Field.FIELD_HCENTER | Field.FOCUSABLE);
		grupos.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color, LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		grupos.setChangeListener(groupsListener);
		vfm.add(grupos);

		// Separator
		vfm.add(new LabelField(""));
		add(vfm);
	}

	private FieldChangeListener newsListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			if (new RefreshData().mustCheckWs(WebServiceURLs.WS_KEY_NEWS, course.getIDAula())){
				PleaseWaitPopupScreen.showScreenAndWait(new FilesAndNewsRunnable(course,Controller.getInstance().getHash(),"NEWS"), resources.getString(POPUP_LOADING));
			} else {
				Controller.getInstance().openNewsScreen(course.getNews());
			}
		}
	};

	private FieldChangeListener groupsListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			// etapa 3: uncomment for etapa 3: 
			PleaseWaitPopupScreen.showScreenAndWait(new GroupRunnable(course, Controller.getInstance().getHash(),false), resources.getString(POPUP_LOADING));
			//Dialog.alert("Proximamente...");//resources.getString(ALERT_COMING_SOON));
		}
	};

	private FieldChangeListener downloadsListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			if (new RefreshData().mustCheckWs(WebServiceURLs.WS_KEY_FILES, course.getIDAula())){
				PleaseWaitPopupScreen.showScreenAndWait(new FilesAndNewsRunnable(course,Controller.getInstance().getHash(),"DOWNLOADS"), resources.getString(POPUP_LOADING));
			} else {
				Controller.getInstance().openDownloadsScreen(course.getFolders());
			}
		}
	};
	
}
